package com.pet.controller;

import com.pet.pojo.User;
import com.pet.service.UserService;
import com.pet.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/*  刘泽  */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //查询用户信息是否存在
    @PostMapping("/login")
    public SysResult login(User user){
        String result = userService.login(user);
        if(result.equals("用户名不存在")) return SysResult.fail(result);
        if(result.equals("密码不正确")) return SysResult.fail(result);
        return SysResult.success(user.getUsername(),result);
    }

    //注册用户信息
    @PostMapping("/register")
    public SysResult register(String username,String password,String repassword,String phone,String email){
        //判断两次密码输入是否一致
        if(!password.equals(repassword)) return SysResult.fail("两次输入密码不一致，请重新输入");
        User user = new User().setUsername(username).setEmail(email).setPassword(password).setStatus(0).setPhone(phone);
        //进入service层实现注册业务
        Boolean flag = userService.register(user);
        //如果返回false，说明用户名以被注册
        if(!flag) return SysResult.fail("用户名已注册，请重新登录");
        return SysResult.success();
    }
}
